﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace System8ToyMC
{
	public partial class SaveLoad : UserControl
	{
		public SaveLoad()
		{
			// Required to initialize variables
			InitializeComponent();
            LayoutRoot.DataContext = this;
		}

        /// <summary>
        /// Get/Set the list of names we already have saved and know about
        /// </summary>
        [Category("Numbers")]
        [Description("The list of the saved names")]
        public ObservableCollection<string> SavedNames
        {
            get { return (ObservableCollection<string>)GetValue(SavedNamesProperty); }
            set { SetValue(SavedNamesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SavedNames.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SavedNamesProperty =
            DependencyProperty.Register("SavedNames", typeof(ObservableCollection<string>), typeof(SaveLoad), new PropertyMetadata(null));

        [Category("Numbers")]
        [Description("Enable the Save button")]
        public bool EnableSaveButton
        {
            get { return (bool)GetValue(EnableSaveButtonProperty); }
            set { SetValue(EnableSaveButtonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EnableSaveButton.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EnableSaveButtonProperty =
            DependencyProperty.Register("EnableSaveButton", typeof(bool), typeof(SaveLoad), new PropertyMetadata(false));


        /// <summary>
        /// Fired when the user wants to load some saved data.
        /// </summary>
        public event Action<string> LoadSavedData;

        /// <summary>
        /// Fired when the user wants to save some data.
        /// </summary>
        public event Action<string> SaveData;

        /// <summary>
        /// User wants to load!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoLoad(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DatasetName.Text == "")
            {
                return;
            }

            if (SavedNames.Contains(DatasetName.Text))
            {
                if (LoadSavedData != null)
                {
                    LoadSavedData(DatasetName.Text);
                }
            }
        }

        /// <summary>
        /// Do the save of the thing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoSave(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DatasetName.Text == "")
            {
                return;
            }

            if (SaveData != null)
            {
                SaveData(DatasetName.Text);
            }
        }        
	}
}